package com.view
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	public class LoadSprite extends Sprite
	{
		private var loadText:TextField;
		private var loader:Loader;
		private var loadURL:String;
		private var data:Object;
		private var callFunc:Function;
		public function LoadSprite()
		{
			var tf:TextFormat = new TextFormat("宋体",15,0xff0000,true);
			tf.leading = 10;
			loadText = new TextField;
			addChild(loadText);
			loadText.type = TextFieldType.DYNAMIC;
			loadText.defaultTextFormat = tf;
			loadText.width = 400;
			loadText.height = 50;
			loadText.selectable = false;
			loadText.text = "";
			this.addEventListener(Event.ADDED_TO_STAGE,onEnter);
		}
		public function loadData(url:String ,fun:Function):void
		{
			if(url == null)
				return ;
			loadURL = url;
			callFunc = fun;
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgerss);
			loader.load(new URLRequest(loadURL));
		}
		private function onEnter(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onEnter);
			loadText.x = (stage.stageWidth - this.width) / 2;
			loadText.y = (stage.stageHeight - this.height) / 2;
		}
		private function onComplete(e:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,onProgerss);
			
			data = (loader.content as Bitmap).bitmapData;
			if(callFunc != null)
				callFunc.call(null,data);
			
			
		}
		private function onProgerss(e:ProgressEvent):void
		{
			loadText.text = "当前进度: " + int(e.bytesLoaded / e.bytesTotal) * 100 + " % "; 
		}
		
	}
}